/*
 * Copyright (c) 2016. 恒昌互联网运营中心.
 */

package com.credithc.eagleeye.api.factory;
import com.credithc.eagleeye.api.config.ESMonitorConfig;
import com.credithc.eagleeye.api.utils.SpringContextUtils;
import com.credithc.framework.es.util.ElasticSearchConfiguration;
import com.credithc.framework.es.util.ElasticSearchUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 描 述: <br/>
 * 作 者: zhangjin<br/>
 * 创 建：2016年06月21日<br/>
 * 版 本：v1.0.0<br>
 * <p/>
 * 历 史: (版本) 作者 时间 注释 <br/>
 */
@SuppressWarnings("Duplicates")
public class ESFactory {
    private final static Logger logger = LoggerFactory.getLogger(ESFactory.class);

    private static ElasticSearchUtil instance;


    public static ElasticSearchUtil getInstance(){
        if (instance == null) {
            synchronized (ESFactory.class) {
                if (instance == null) {
                    ElasticSearchConfiguration esConfig=new ElasticSearchConfiguration();
                    ESMonitorConfig monitorConfig = SpringContextUtils.getBean(ESMonitorConfig.class);
                    assert monitorConfig != null;
                    esConfig.setAddress(monitorConfig.getAddress());
                    esConfig.setClusterName(monitorConfig.getClusterName());
                    esConfig.setShieldUser(monitorConfig.getShieldUser());
                    ElasticSearchUtil esUtil = new ElasticSearchUtil(esConfig);
                    logger.info(String.format("eagle-eye监控ES地址:%s",esConfig));
                    instance = esUtil;
                }
            }
        }
        return instance;

    }

}
